package com.xm.cloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xm.cloud.user.entity.User;
import com.xm.cloud.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 新增用户
     */
    @PostMapping
    public boolean save(@RequestBody User user) {
        return userService.save(user);
    }

    /**
     * 根据 ID 删除用户
     */
    @DeleteMapping("/{id}")
    public boolean removeById(@PathVariable Long id) {
        return userService.removeById(id);
    }

    /**
     * 更新用户信息
     */
    @PutMapping
    public boolean updateById(@RequestBody User user) {
        return userService.updateById(user);
    }

    /**
     * 根据 ID 查询用户
     */
    @GetMapping("/{id}")
    public User getById(@PathVariable Long id) {
        return userService.getById(id);
    }

    /**
     * 查询所有用户
     */
    @GetMapping
    public List<User> list() {
        return userService.list();
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public IPage<User> page(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return userService.page(new Page<>(pageNum, pageSize));
    }
}
